﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;

namespace ACM.MES.工序
{
    public class OperationManager : DomainService
    {
        private readonly IOperationRepository _operationRepository;

        public OperationManager(IOperationRepository operationRepository)
        {
            _operationRepository = operationRepository;
        }
        /// <summary>
        /// 创建工序
        /// </summary>
        /// <param name="opCode">工序编号</param>
        /// <param name="description">工序名称</param>
        /// <param name="remark">备注</param>
        /// <returns></returns>
        /// <exception cref="OperationAlreadyExistsException"></exception>
        public async Task<Operation> CreateAsync(
        string opCode,
        string description,
        string remark)
        {
            Check.NotNullOrWhiteSpace(opCode, nameof(opCode));

            var existingOperation = await _operationRepository.FindByOpCodeAsync(opCode);
            if (existingOperation != null)
            {
                throw new OperationAlreadyExistsException(opCode);
            }

            return new Operation(
                GuidGenerator.Create(),
                opCode,
                description,
                remark
            );
        }
    }
}
